package pe.gob.mininter.dirandro.vaadin.panel.bandeja;

import org.apache.log4j.Logger;

import pe.gob.mininter.dirandro.model.Informe;
import pe.gob.mininter.dirandro.model.Pericia;
import pe.gob.mininter.dirandro.service.InformeService;
import pe.gob.mininter.dirandro.service.PericiaService;
import pe.gob.mininter.dirandro.util.HarecUtil;
import pe.gob.mininter.dirandro.vaadin.dialogs.AlertDialog;
import pe.gob.mininter.dirandro.vaadin.util.Injector;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

public class PanelInformeComplementario extends CustomComponent {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_1;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private Button btnCancelar;
	@AutoGenerated
	private Button btnRegistrar;
	@AutoGenerated
	private Label lblEstado;
	@AutoGenerated
	private Label lblMuestra;
	@AutoGenerated
	private Label label_1;
	/**
	 * 
	 */
	private static final long serialVersionUID = -7946340288183604731L;
	private static final Logger logger = Logger.getLogger(PanelInformeComplementario.class);
	private InformeService informeService;
	private Informe informe;
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public PanelInformeComplementario() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

		informeService = Injector.obtenerServicio(InformeService.class);
		
		btnRegistrar.addListener(new ClickListener() {
			
			private static final long serialVersionUID = 4539783663387871432L;

			@Override
			public void buttonClick(ClickEvent event) {
				
				logger.debug(" en el registro......... cerrar la pantalla ");

				if(informe.esNuevo()){
					informeService.crear( informe );	
				}
				
				AlertDialog alertDialog = new  AlertDialog("Registro de Informe Complementario", "Se ha registrado el Informe Complementario correctamente", "Aceptar", "400");
				getApplication().getMainWindow().addWindow(alertDialog);
				
	            getApplication().getMainWindow().removeWindow(getWindow());
				
			}
			
		});
		
		btnCancelar.addListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
			    getApplication().getMainWindow().removeWindow(getWindow());
			}
		});
	}

	public Informe getInforme() {
		return informe;
	}

	public void setInforme(Informe informe) {
		this.informe = informe;
		
		if( informeService.buscar(informe).size()!=0){
			informe = informeService.buscar(informe).get(0);
			btnRegistrar.setVisible(false);
			label_1.setValue("Ya existe un informe complementario.");
			btnCancelar.setCaption("Cerrar");
		}
		
		String muestra = "";//pericia.getMuestra().getDroga()!=null?"Droga: ":"Especie: " ;
		//muestra += pericia.getMuestra().getDroga()!=null? HarecUtil.marcaModeloNombreToEmpty( pericia.getMuestra().getDroga().getTipoDroga() ) : pericia.getMuestra().getEspecie().getNombre();
		
		lblMuestra.setValue( muestra );
		lblEstado.setValue( "Estado del Informe: "+informe.getNumero() );
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// verticalLayout_1
		verticalLayout_1 = buildVerticalLayout_1();
		mainLayout.addComponent(verticalLayout_1);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_1() {
		// common part: create layout
		verticalLayout_1 = new VerticalLayout();
		verticalLayout_1.setImmediate(false);
		verticalLayout_1.setWidth("450px");
		verticalLayout_1.setHeight("-1px");
		verticalLayout_1.setMargin(false);
		verticalLayout_1.setSpacing(true);
		
		// label_1
		label_1 = new Label();
		label_1.setImmediate(false);
		label_1.setWidth("-1px");
		label_1.setHeight("-1px");
		label_1.setValue("Desea solicitar la pericia de lo siguiente:");
		verticalLayout_1.addComponent(label_1);
		
		// lblMuestra
		lblMuestra = new Label();
		lblMuestra.setImmediate(false);
		lblMuestra.setWidth("-1px");
		lblMuestra.setHeight("-1px");
		lblMuestra.setValue("Detalle de la pericia");
		verticalLayout_1.addComponent(lblMuestra);
		
		// lblEstado
		lblEstado = new Label();
		lblEstado.setImmediate(false);
		lblEstado.setWidth("-1px");
		lblEstado.setHeight("-1px");
		lblEstado.setValue("Label");
		verticalLayout_1.addComponent(lblEstado);
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		verticalLayout_1.addComponent(horizontalLayout_1);
		
		return verticalLayout_1;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("100.0%");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		
		// btnRegistrar
		btnRegistrar = new Button();
		btnRegistrar.setCaption("Registrar");
		btnRegistrar.setImmediate(true);
		btnRegistrar.setWidth("-1px");
		btnRegistrar.setHeight("-1px");
		horizontalLayout_1.addComponent(btnRegistrar);
		horizontalLayout_1.setComponentAlignment(btnRegistrar,
				new Alignment(34));
		
		// btnCancelar
		btnCancelar = new Button();
		btnCancelar.setCaption("Cacenlar");
		btnCancelar.setImmediate(true);
		btnCancelar.setWidth("-1px");
		btnCancelar.setHeight("-1px");
		horizontalLayout_1.addComponent(btnCancelar);
		horizontalLayout_1
				.setComponentAlignment(btnCancelar, new Alignment(33));
		
		return horizontalLayout_1;
	}

}
